package com.qf.personal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.personal.entiy.Holiday;
import com.qf.personal.entiy.Things;
import com.qf.personal.mapper.HolidayMapper;
import com.qf.personal.mapper.IndexMapper;
import com.qf.personal.service.HolidayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;


@Service
public class HolidayServiceImpl implements HolidayService {
    @Resource
    private HolidayMapper holidayMapper;
    @Resource
    private IndexMapper indexMapper;

    @Override
    @Transactional
    public RespResult<String> addHoliday(Holiday holiday, int mId, int eId, int tId) {
        Long id = IdWorker.getId();
        holiday.setHolidayId(id);
        Things things = new Things();
        try {
            int a = holidayMapper.add(holiday);
            if (a != 0) {
                holidayMapper.addCorrelation(id, mId, eId, tId);
                things.setId(id);
                things.setLName(indexMapper.selectNameByLId(2));
                things.setTName(indexMapper.selectNameByTId(tId));
                things.setMName(indexMapper.selectNameByMId(mId));
                things.setEName(indexMapper.selectNameByEId(eId));
                things.setEId(eId);
                things.setTime(new Date());
                indexMapper.add(things);
                return RespResult.success("新增成功");
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return RespResult.success("新增成功");
    }

    @Override
    @Transactional
    public RespResult<String> deleteById(Long id) {
        int a = holidayMapper.delete(id);
        try {
            if (a == 1) {
                int b = indexMapper.delete(id);
                if (b == 1) {
                    return RespResult.success("删除成功");
                }
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return null;
    }
}
